package com.yunqian.auth;

import com.yunqian.jwt.JwtUserFactory;
import com.yunqian.redot.domain.Customers;
import com.yunqian.redot.repository.CustomersRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * @author zhaoxinguo on 2017/9/13.
 */
@Service
public class CustomerDetailsServiceImpl implements UserDetailsService {

    private CustomersRepository customersRepository;

    /**
     * 通过构造器注入 CustomersRepository
     *
     * @param customersRepository
     */
    public CustomerDetailsServiceImpl(CustomersRepository customersRepository) {
        this.customersRepository = customersRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        Customers customers = customersRepository.findByPhone(phone);
        if (customers == null) {
            throw new UsernameNotFoundException(phone);
        }
        return JwtUserFactory.create(customers);
    }

}

